package com.hspedu.newData.dmsxl.数组.前缀和;
/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-20 
 * @Version: 1.0
 */

public class LC2574左右元素和的差值 {

    public int[] leftRightDifference(int[] nums) {
        int len = nums.length;
        int[] left = new int[len];
        int[] right = new int[len];

        for (int i = 1; i < len; i++) {
            left[i] = left[i-1] + nums[i-1];
        }

        for (int i = len-2; i >= 0; i--) {
            right[i] = right[i+1] + nums[i+1];
        }

        int[] res = new int[len];
        for (int i = 0; i < len; i++) {
            res[i] = Math.abs(left[i] - right[i]);
        }

        return res;
    }

}
